package com.wiken.userservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.R;
import com.wiken.servicecommon.common.TokenUtil;
import com.wiken.userservice.entity.User;
import com.wiken.userservice.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/userservice/user")
@Api(value = "API")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @PostMapping("/register")

    public R register(@RequestBody  User user){
        userService.register(user);

        return R.ok().message("注册成功").code(Code.SUCCESS);
    }

    @PostMapping("/login")
    public R login(@RequestBody  User user){
        String token = userService.login(user);

        return R.ok().message("登陆成功").code(Code.SUCCESS).data("token", token);
    }

    @PostMapping("/getUserByUserId")
    public R getUserByUserId(@RequestBody String userId){
        User user = userService.getUserByUserId(userId);

        return R.ok().code(200).data("user", user);
    }

    @GetMapping("/getUser")
    public R getUser(@RequestHeader("Authorization") String token){
        String userId = TokenUtil.getUserIdByToken(token);

        User user = userService.getUserByUserId(userId);

        return R.ok().code(200).data("user", user);
    }

    @PostMapping("/getUsers")
    public R getUsers(@RequestBody Page page){
        if(page == null) page = new Page();

        Page<User> userPage = userService.allUser(page);

        return R.ok().code(200).data("userList", userPage);
    }
}
